<template>
  <div>
    <h1>登录</h1>
    <div style="width: 1000px; height: 500px; margin-left: 400px;">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
        <el-form-item label="账户" prop="name">
          <el-input v-model="loginForm.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="loginForm.pass"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">立即登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loginForm: {
          name: '',
          pass:''
        },
        rules: {
          name: [
            { required: true, message: '请输入账号', trigger: 'blur' },
          ],
          pass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            //json中用的是 weixin
            let username = this.loginForm.name;

            this.$axios.get('../../static/login.json').then( response => {
              let data = response.data;
              if (data.name == username) {
                this.$router.push({ path : '/main'})
              } else {
                alert('NO');
              }

            }).catch(error => {
              console.log(error);
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },

      /**
       * 重置
       * @param formName
       */
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
